<?php
include("include/conn.php");
require('include/Smarty/libs/Smarty.class.php');

$kaarigar = array();
$total    = array();
$crAmountTotal      = 0;
$drAmountTotal      = 0;
$total['vadhGhat']  = 0;
$total['drMinusCr'] = 0;
$total['addVadhaaroGhataado']    = 0;
		

$selectKaarigar = "SELECT kaarigarId,kaarigarName FROM kaarigar
                    ORDER BY kaarigarName";
$selectKaarigarRes = mysql_query($selectKaarigar);
$a = 0;
while($row = mysql_fetch_assoc($selectKaarigarRes))
{
	$kaarigar[$a]['id']       = $row['kaarigarId'];
	$kaarigar[$a]['name']     = $row['kaarigarName'];
	$kaarigar[$a]['crAmount'] = 0;
	$kaarigar[$a]['drAmount'] = 0;
	$kaarigar[$a]['vadhaaroGhatado']     = 0;
  $kaarigar[$a]['addVadhaaroGhataado'] = 0;
  $kaarigar[$a]['crAmount']  = 0;
  $kaarigar[$a]['drAmount']  = 0;
  $kaarigar[$a]['drMinusCr'] = 0;

	$selectKaarigarDetail = "SELECT kaarigardetail.vadhaaroGhatado, kaarigardetail.addVadhaaroGhataado, kaarigardetail.crAmount,kaarigardetail.drAmount 
	                           FROM kaarigardetail
	                           JOIN kaarigarmaster
	                          WHERE kaarigarmaster.kaarigarMasterId = kaarigardetail.kaarigarMasterId
	                            AND kaarigarmaster.kaarigarId = ".$row['kaarigarId']."
	                          ORDER BY kaarigarDate, kaarigardetailId";
	$selectKaarigarDetailRes = mysql_query($selectKaarigarDetail);
	while($kaarigarDetailRow = mysql_fetch_assoc($selectKaarigarDetailRes))
	{
		$kaarigar[$a]['vadhaaroGhatado']     += $kaarigarDetailRow['vadhaaroGhatado'];
		$kaarigar[$a]['addVadhaaroGhataado'] += $kaarigarDetailRow['addVadhaaroGhataado'];
		$kaarigar[$a]['crAmount']  += $kaarigarDetailRow['crAmount'];
		$kaarigar[$a]['drAmount']  += $kaarigarDetailRow['drAmount'];
		$kaarigar[$a]['drMinusCr'] += $kaarigarDetailRow['drAmount'] - $kaarigarDetailRow['crAmount'];
		
	}
	
	$total['addVadhaaroGhataado']  += $kaarigar[$a]['addVadhaaroGhataado'];
	$total['vadhGhat']  += $kaarigar[$a]['vadhaaroGhatado'];
	$total['drMinusCr'] += $kaarigar[$a]['drAmount'] - $kaarigar[$a]['crAmount'];
	$crAmountTotal      += $kaarigar[$a]['crAmount'];
	$drAmountTotal      += $kaarigar[$a]['drAmount'];
	$a++;
}

$smarty = new Smarty;
require("bottom.php");

$smarty->assign("l_kaarigarName",$l_kaarigarName);
$smarty->assign("l_crAmount",$l_crAmount);
$smarty->assign("l_drAmount",$l_drAmount);
$smarty->assign("l_total",$l_total);
$smarty->assign("crAmountTotal",$crAmountTotal);
$smarty->assign("drAmountTotal",$drAmountTotal);

$smarty->assign("kaarigar",$kaarigar);
$smarty->assign("total",$total);
$smarty->display("kaarigarSummery.tpl");
?>